home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15258 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  72 lines

  1. Path: news.ultranet.com!usenet
  2. From: seedy@ultranet.com
  3. Newsgroups: gnu.g++.help,comp.sys.lang.c++,comp.lang.c++
  4. Subject: Help needed on function templates
  5. Date: Thu, 04 Apr 96 19:17:06 PDT
  6. Organization: UltraNet Communications, Inc.
  7. Message-ID: <NEWTNews.828674472.23501.seedy@trigent.trigent.com>
  8. NNTP-Posting-Host: trigent.ultranet.com
  9. Mime-Version: 1.0
  10. Content-Type: TEXT/PLAIN; charset=US-ASCII
  11. X-Newsreader: NEWTNews & Chameleon -- TCP/IP for MS Windows from NetManage
  12.  
  13.  
  14. When attempted to compile the following code I got the following error :
  15. I use g++ (2.7.1) on HP-UX 10.x.
  16.  
  17. news.cc: In function `int equal(const class abc &, const class abc &)':
  18. news.cc:23: no match for `operator ==(class abc, class abc)'
  19.  
  20. #include <iostream.h>
  21.  
  22. class abc {
  23.     public : 
  24.        abc( int x )
  25.       {
  26.       a = x;
  27.       b = 0;
  28.       }
  29.        int a;
  30.        int b;
  31.        inline int operator ==(abc x)
  32.            {
  33.               return ((a==x.a) && (b==x.b));
  34.            }
  35.     };
  36.  
  37.  
  38.  
  39. template < class Element>
  40. inline int equal (Element const& e1, Element const& e2) 
  41. {
  42.   return e1 == e2;
  43.  
  44. }
  45.  
  46. main()
  47. {
  48. abc a1(5), a2(5);
  49.  
  50. cout << "Return value is = " << equal(a1, a2) << endl;
  51. }
  52.  
  53.  
  54. Even though the 'operator ==' is overloaded in class abc, compiler is
  55. reporting match not found. By making the overloaded function 'operator =='
  56. as friend to 'class abc' I could resolve the problem, because in this case,
  57. this overloaded function becomes outsider function.
  58.  
  59. As far as I know, template instantiation for function 'equal will be done
  60. for 'class abc'(because the passed parameters are of type class abc), 
  61. and so the corresponding member function 'operator ==' can be used for 
  62. template variables e1 and e2.
  63.  
  64. Any explanations?  Please forward your response to 'seedy@trigent.com'
  65. or post it here.
  66.  
  67. Regards,
  68.  
  69. Praveen.
  70.  
  71.  
  72.